<?php

namespace App\Console\Commands;

use App\Models\PermissionCategory;
use App\Models\Role;
use App\Models\User;
use Illuminate\Console\Command;

class CreateUsers extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:users';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'create basic users';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $q = $this->ask('create basic user and role?','n');
        if(strtolower($q)=='y'){
            $user_rec1 = User::create([
                    'name'=>'抄表员1',
                    'email'=>'recorder1@me.com',
                    'active'=>'1',
                    'password'=>\Hash::make('123456'),
                ]
            );
            $user_rec2 = User::create([
                    'name'=>'抄表员2',
                    'email'=>'recorder2@me.com',
                    'active'=>'1',
                    'password'=>\Hash::make('123456'),
                ]
            );
            $user_rec3 = User::create([
                    'name'=>'抄表员3',
                    'email'=>'recorder3@me.com',
                    'active'=>'1',
                    'password'=>\Hash::make('123456'),
                ]
            );
            $user_rec4 = User::create([
                    'name'=>'抄表员4',
                    'email'=>'recorder4@me.com',
                    'active'=>'1',
                    'password'=>\Hash::make('123456'),
                ]
            );
            $user_rec5 = User::create([
                    'name'=>'抄表员5',
                    'email'=>'recorder5@me.com',
                    'active'=>'1',
                    'password'=>\Hash::make('123456'),
                ]
            );
            $user_op = User::create([
                    'name'=>'操作员',
                    'email'=>'user@me.com',
                    'active'=>'1',
                    'password'=>\Hash::make('123456'),
                ]
            );
            $user_admin = User::create([
                    'name'=>'管理员',
                    'email'=>'admin@me.com',
                    'active'=>'1',
                    'password'=>\Hash::make('123456'),
                ]
            );

            $role_rec = Role::create([
                    'name'=>'记录员',
                    'display_name'=>'记录员',
                    'active'=>'1',
                    'description'=>'可以管理人员，宿舍',
                ]
            );
            $user_rec1->roles()->sync([$role_rec->id]);
            $user_rec2->roles()->sync([$role_rec->id]);
            $user_rec3->roles()->sync([$role_rec->id]);
            $user_rec4->roles()->sync([$role_rec->id]);
            $user_rec5->roles()->sync([$role_rec->id]);

            $role_op = Role::create([
                    'name'=>'操作员',
                    'display_name'=>'操作员',
                    'description'=>'可以管理人员，宿舍和费用',
                ]
            );
            $user_op->roles()->sync([$role_op->id]);

            $role_admin = Role::create([
                    'name'=>'管理员',
                    'display_name'=>'管理员',
                    'description'=>'可以管理人员，宿舍和费用和基础数据',
                ]
            );
            $user_admin->roles()->sync([$role_admin->id]);


            $cats = PermissionCategory::all();

            $admin_except = ['用户','角色','权限','权限类型','用户管理'];
            $admin_perm_cats = $cats->filter(function ($v)use($admin_except){
                return in_array($v->name,$admin_except)==false;
            });
            $admin_perm_ids = $admin_perm_cats->map(function($cat){
                return $cat->permissions->pluck('id');
            })->flatten()->all();

            $role_admin->perms()->sync($admin_perm_ids);

            $op_except = ['用户','角色','权限','权限类型','用户管理','基础数据'];
            $op_perm_cats = $cats->filter(function ($v)use($op_except){
                return in_array($v->name,$op_except)==false;
            });
            $op_perm_ids = $op_perm_cats->map(function($cat){
                return $cat->permissions->pluck('id');
            })->flatten()->all();
            $role_op->perms()->sync($op_perm_ids);

            $rec_except = ['用户','角色','权限','权限类型','用户管理','基础数据','费用管理'];
            $rec_perm_cats = $cats->filter(function ($v)use($rec_except){
                return in_array($v->name,$rec_except)==false;
            });
            $rec_perm_ids = $rec_perm_cats->map(function($cat){
                return $cat->permissions->pluck('id');
            })->flatten()->all();
            $role_rec->perms()->sync($rec_perm_ids);
        }
    }
}
